﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;

using System;
using System.Collections.Generic;
using System.Net;

namespace CatUtils.Utils.SpecialUtils.PicBed.Sina
{
    /// <summary>
    /// 图床帮助方法
    /// </summary>
    public class PicBedHelper
    { /// <summary>
      /// 需要被锁定的常见失效图片大小和尺寸
      /// </summary>
        public static List<WebHelper.BlockImgInfo> BlockImgInfo = new List<WebHelper.BlockImgInfo> {
                new WebHelper.BlockImgInfo{  FileSize=8844, ImgHeight=480, ImgWidth=360},
                new WebHelper.BlockImgInfo{  FileSize=8308, ImgHeight=360, ImgWidth=480},
                  new WebHelper.BlockImgInfo{  FileSize=8271, ImgHeight=360, ImgWidth=480},
                  new WebHelper.BlockImgInfo{  FileSize=7092, ImgHeight=360, ImgWidth=360}
            };

        /// <summary>
        ///  检查新浪登录
        /// </summary>
        public static void CheckSinaLogin()
        {
            if (GetLoginStatus())
                using (SinaLoginForm Sina = new SinaLoginForm("https://weibo.com/"))

                {
                    Sina.ShowDialog();
                }
        }

        /// <summary>
        /// 获取登录状态 如果需要登录返回true
        /// </summary>
        /// <returns></returns>
        private static bool GetLoginStatus()
        {
            string UploadImg = SinaImgUploadTool.UploadImgLink("http://ww2.sinaimg.cn/orj360/006rcAdrgy1fvuuap85uaj30m80pljtw.jpg");
            // UploadImgLink("");
            if (UploadImg == "")
                return true;
            return false;
        }

        /// <summary>
        /// 替换文章图片到外链
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ReplcaeContentImg(string Content)
        {
            return ReplcaeContentImg(Content, WebHelper.PcWebHeader());
        }

        /// <summary>
        /// 替换文章图片到外链
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="DownloadImgHeader"></param>
        /// <returns></returns>
        public static string ReplcaeContentImg(string Content, WebHeaderCollection DownloadImgHeader)
        {
            List<string> ImgList = HtmlHelper.GetImgSrcVlaueList(Content, "//img");

            foreach (string line in ImgList)
            {
                string NewImgLink = UPLoadPicBedInMemory(line, DownloadImgHeader);
                Console.WriteLine("获取到图片" + NewImgLink);
                if (NewImgLink != "")
                {
                    Content = Content.Replace(line, NewImgLink);
                }
            }
            return Content;
        }

        /// <summary>
        /// 上传图片并且返回链接   上传失败返回""  在内存中完成读写
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <param name="WebHeaderCollection">特定的header，辅助下载</param>
        /// <returns></returns>
        public static string UPLoadPicBedInMemory(string Imgurl)
        {
            return UPLoadPicBedInMemory(Imgurl, WebHelper.PcWebHeader());
        }

        /// <summary>
        /// 上传图片并且返回链接   上传失败返回""  在内存中完成读写
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <param name="WebHeaderCollection">特定的header，辅助下载</param>
        /// <returns></returns>
        public static string UPLoadPicBedInMemory(string Imgurl, WebHeaderCollection WebHeaderCollection)
        {
            if (ListHelper.CompareRepetition(Imgurl, new List<string> { "sinaimg.cn" }))//"i.loli.net",
            {
                Console.WriteLine("跳出！");
                return Imgurl;
            }

            string TempSinaImgUrl = UploadByteAuto(WebHelper.ByteDownload(Imgurl, WebHeaderCollection));

            return TempSinaImgUrl;
        }

        /// <summary>
        /// 自动选择上传Byte的方式
        /// </summary>
        /// <param name="ImgData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public static string UploadByteAuto(byte[] ImgData)
        {
            if (ImgData == null)
                return "";
            string TempSinaImgUrl = SinaImgUploadTool.UploadByte(ImgData);

            if (TempSinaImgUrl == "")
                Console.WriteLine("UploadERROR");
            //  TempSinaImgUrl = Smms.UploadByte(ImgData);

            return TempSinaImgUrl;
        }

        /// <summary>
        /// 上传图片并且返回链接   上传失败返回""
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <returns></returns>
        public static string UploadImgLink(string Imgurl)
        {
            string TempSinaImgUrl = UploadImgLink(Imgurl, WebHelper.PcWebHeader());

            return TempSinaImgUrl;
        }

        /// <summary>
        /// 上传图片并且返回链接   上传失败返回""
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <param name="WebHeaderCollection">特定的header，辅助下载</param>
        /// <returns></returns>
        public static string UploadImgLink(string Imgurl, WebHeaderCollection WebHeaderCollection)
        {
            string TempSinaImgUrl = "";

            string FilePath = WebHelper.DownloadToFile(Imgurl, FileNameHelper.CreateTempFileName() + ".jpg");
            if (FilePath == "")
            {
                return Imgurl;
            }

            TempSinaImgUrl = UploadFile(FilePath);
            FileHelper.FileDelete(FilePath);
            return TempSinaImgUrl;
        }

        /// <summary>
        /// 上传本地文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string UploadFile(string FilePath)
        {
            string TempSinaImgUrl = "";

            if (TempSinaImgUrl == "")
            {
                TempSinaImgUrl = SinaImgUploadTool.UploadFile(FilePath);
            }

            return
                TextHelper.ReplaceTextEnhance(TempSinaImgUrl, "((https|http|)://w(w|s|x)[0-9]{1}\\.sinaimg\\.cn|//w(w|s|x)[0-9]{1}\\.sinaimg\\.cn)"
                    , "http://tva1.sinaimg.cn");
        }
    }
}